/*
 *    Copyright (c) 2018-2025, fanxl All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: fanxl (xunlian_fan001@126.com)
 */

package com.taiji.dicom4cloud.smart.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.util.StringUtil;
import com.taiji.dicom4cloud.common.core.utils.CommonUtils;
import com.taiji.dicom4cloud.common.core.utils.R;
import com.taiji.dicom4cloud.common.log.annotation.SnowLog;
import com.taiji.dicom4cloud.smart.model.entity.Car;
import com.taiji.dicom4cloud.smart.model.entity.CarOwer;
import com.taiji.dicom4cloud.smart.model.entity.Org;
import com.taiji.dicom4cloud.smart.service.CarOwerService;
import com.taiji.dicom4cloud.smart.service.CarService;
import com.taiji.dicom4cloud.smart.service.OrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 
 *
 * @author fanxl
 * @date 2021-07-06 14:01:20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/car" )
@Api(value = "car", tags = "管理")
public class CarController {

    private final CarService carService;

    private final CarOwerService carOwerService;

    private final OrgService orgService;
    
    /**
     * 分页查询
     * @param page 分页对象
     * @param car 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getCarPage(Page page, Car car) {
        return R.ok(carService.queryPage(page,car));
    }


    /**
     * ownerId
     * @param ownerId id
     * @return R
     */
    @ApiOperation(value = "ownerId", notes = "ownerId")
    @GetMapping("/ownerId/{ownerId}" )
    public R getByOwnerId(@PathVariable("ownerId" ) String ownerId) {
        return R.ok(carService.list(Wrappers.<Car>lambdaQuery().eq(Car::getOwnerId,ownerId)
        .eq(Car::getDelFlag,"0")));
    }

    /**
     * 通过id查询
     * @param carId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{carId}" )
    public R getById(@PathVariable("carId" ) String carId) {
        return R.ok(carService.getById(carId));
    }

    /**
     * 新增
     * @param car 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SnowLog("新增" )
    @PostMapping
    public R save(@RequestBody Car car) {
        return R.ok(carService.saveCar(car));
    }

    /**
     * 修改
     * @param car 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SnowLog("修改" )
    @PutMapping
    public R updateById(@RequestBody Car car) {
        return R.ok(carService.updateById(car));
    }

    /**
     * 通过id删除
     * @param carId id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SnowLog("通过id删除" )
    @DeleteMapping("/{carId}" )
    public R removeById(@PathVariable String carId) {
        Car car = carService.getById(carId);
        car.setDelFlag("1");
        return R.ok(carService.saveOrUpdate(car));
    }

    @PostMapping("/bind")
    public R bindRfidCardNo(@RequestBody Car car){
        return R.ok(carService.bindRfidCardNo(car));
    }

}
